package com.example.paper.fragment;

import android.Manifest;
import android.content.pm.PackageManager;
import android.os.Bundle;

import androidx.core.app.ActivityCompat;
import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.PhoneUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.example.db.DBManager;
import com.example.db.Goods;
import com.example.mvp.model.BaseEntity;
import com.example.mvp.view.BaseFragment;
import com.example.paper.R;
import com.example.paper.adapter.NewsVpAdapter;
import com.example.paper.adapter.TextBannerAdapter;
import com.example.paper.brean.BannerBean;
import com.example.paper.brean.ChannelBean;
import com.example.paper.brean.HttpCode;
import com.example.paper.brean.TextBannerBean;
import com.example.paper.mvp.contract.LREContract;
import com.example.paper.mvp.model.LREModel;
import com.example.paper.mvp.presenter.LREPresenter;
import com.example.paper.mvp.view.news.ContentNewsFragment;
import com.flyco.tablayout.CommonTabLayout;
import com.flyco.tablayout.SlidingTabLayout;
import com.youth.banner.Banner;
import com.youth.banner.listener.OnBannerListener;
import com.youth.banner.transformer.ZoomOutPageTransformer;

import org.w3c.dom.Text;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


public class NewsFragment extends BaseFragment<LREPresenter> implements LREContract.ILREView, OnBannerListener<TextBannerBean.Values> {
    private SlidingTabLayout tabLayout;
    private ViewPager viewPager;
    private Banner<TextBannerBean.Values, TextBannerAdapter> news_text_banner;
    private TextBannerAdapter textBannerAdapter;
    private NewsVpAdapter newsVpAdapter;
    private List<Fragment> fragments;




    @Override
    public int bindLayout() {
        return R.layout.fragment_news;
    }

    @Override
    public void initView() {
        tabLayout = rootView.findViewById(R.id.news_tab);
        viewPager = rootView.findViewById(R.id.news_vp);
        news_text_banner = rootView.findViewById(R.id.news_text_banner);
    }

    @Override
    public void initData() {
        mPresenter = new LREPresenter(new LREModel(),this);
        fragments = new ArrayList<>();
        //获取顶部文字banner数据源
        try {
            List<Goods> users = DBManager.getDbManager().getDaoSession().loadAll(Goods.class);
            boolean flag = users.size() == 0;
            //获取频道数据源
            Map<String, Object> bannerMap = new HashMap<>();
            bannerMap.put("code", HttpCode.TEXTBANNERCODE);
            Map<String, Object> channelMap = new HashMap<>();
            channelMap.put("code", HttpCode.USERCHANNELCODE);
            if (ActivityCompat.checkSelfPermission(getActivity(), Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED) {
                return;
            }
            channelMap.put("imie", flag ? PhoneUtils.getIMEI() : "");
            channelMap.put("userId", 1);
            mPresenter.requestAll(bannerMap, channelMap);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onStart() {
        super.onStart();
        news_text_banner.start();
    }

    @Override
    public void onStop() {
        super.onStop();
        news_text_banner.stop();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        news_text_banner.destroy();
    }

    @Override
    public void refreshAll(BaseEntity entity) {
        if (entity instanceof TextBannerBean){
            //文字banner返回
            TextBannerBean textBannerEntity = (TextBannerBean) entity;
            LogUtils.e("textBannerEntity:"+textBannerEntity.values);
            if (textBannerEntity.getValues() == null) {
                ToastUtils.showShort("文字广告数据丢失");
                return;
            }
            textBannerAdapter = new TextBannerAdapter(textBannerEntity.getValues());
            news_text_banner.setAdapter(textBannerAdapter)
                    .setOrientation(Banner.VERTICAL)
                    .setPageTransformer(new ZoomOutPageTransformer())
                    .setOnBannerListener(this);
        }else if (entity instanceof ChannelBean){
            //频道列表数据
            //按照频道数量以及频道id初始化fragments
            ChannelBean channelEntity = (ChannelBean) entity;
            if (channelEntity.getValues()==null){
                ToastUtils.showShort("无频道数据");
                return;
            }
            for (int i = 0;i < channelEntity.getValues().size();i ++){
                Log.e("asd","添加fragment");
                fragments.add(new ContentNewsFragment(channelEntity.getValues().get(i).channel_id));
            }
            newsVpAdapter = new NewsVpAdapter(getFragmentManager(),fragments,channelEntity.getValues());
            viewPager.setAdapter(newsVpAdapter);
            tabLayout.setViewPager(viewPager);
        }
    }

    @Override
    public void refreshRecyclerView(BaseEntity entity) {

    }

    @Override
    public void loadMoreRecyclerView(BaseEntity entity) {

    }

    @Override
    public void OnBannerClick(TextBannerBean.Values data, int position) {

    }
}